﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameFramework.GameLogic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GameFramework.Camera;

namespace MyRaceGame.GameLogic.GameStates
{
    public class BeginningState : RaceGameState
    {
        public BeginningState(RaceGame raceGame)
            : base(raceGame)
        {
            totalTime = new TimeSpan();
            font = raceGame.Font;
            times = new TimeSpan[] {
                new TimeSpan(0, 0, 1),
                new TimeSpan(0, 0, 2),
                new TimeSpan(0, 0, 3)
            };

            foreach (RacerCharacter racer in RaceController.Racers)
                racer.Speed = 0;
            RaceController.HumanRacer.PlaySpeed = 1f;
            //((ChaseCamera)raceGame.CurrentCamera).PositionOffset = new Vector3(100, 100, 100);
        }

        protected override void UpdateRacers(GameTime gameTime)
        {
            raceGame.UpdateRacers(gameTime, false);
        }

        protected override void UpdateAdditional(GameTime gameTime)
        {
            totalTime += gameTime.ElapsedGameTime;
            currentScale = 50.0f - (totalTime.Milliseconds % 1000) / 20;

            switch (totalTime.Seconds)
            {
                case 0:
                    currentText = "3";
                    break;
                case 1:
                    currentText = "2";
                    break;
                case 2:
                    currentText = "1";
                    break;
                default:
                    raceState = true;
                    break;
            }
        }

        protected override void UpdateTransition(GameTime gameTime)
        {
            if (raceState)
                GameState = new RaceState(raceGame);
        }

        protected override void DrawRacers(GameTime gameTime)
        {
            raceGame.DrawModels();
        }

        protected override void DrawText(GameTime gameTime)
        {
            raceGame.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            raceGame.SpriteBatch.DrawString(font, currentText, getTextPosition(currentText), Color.White, .0f,
                Vector2.Zero, currentScale, SpriteEffects.None, 0.0f);

            raceGame.SpriteBatch.End();
        }

        protected override void DrawingAdditional(GameTime gameTime)
        {
            raceGame.DrawRaceTrack();
        }

        Vector2 getTextPosition(string text)
        {
            RaceGame raceGame = gameStateContext as RaceGame;
            Vector2 textSize = font.MeasureString(text) * currentScale;
            return new Vector2((raceGame.GraphicsDevice.DisplayMode.Width - textSize.X) / 2,
                (raceGame.GraphicsDevice.DisplayMode.Height - textSize.Y) / 2);
        }

        float currentScale = 1.0f;
        string currentText = "";
        TimeSpan totalTime;
        SpriteFont font;
        TimeSpan[] times;
        bool raceState = false;
    }
}
